package com.creative.accountsdk.teacher;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.R;
import com.creative.accountsdk.ThirdLoginActivity;
import com.creative.accountsdk.adapter.CourseAdapter.Callback;
import com.creative.accountsdk.adapter.CourseAdapter;
import com.creative.accountsdk.bean.CourseInfo;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.CourseListProtocol;
import com.creative.accountsdk.net.account.SelectCourseProtocol;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.Header;

/**
 * 教师 - 选择任教
 *
 *
 * @author  David
 * @version 1.0
 * @created 2015-11-25
 */
public class SelectCourseActivity extends ThirdLoginActivity implements OnItemClickListener,Callback {

    private  Dialog      progress;
    private  Context     context;
    private  ListView    lvCurse;
    private  Button      btnOk;
    private CourseAdapter curseAdapter;
    private long   classid;
    private List<CourseInfo> curseList;
    List<Integer> listItemID = new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.teacher_select_curse);

        this.initData();
        this.initview();
        this.setListener();
    }

    private void initData(){
        DataCenter.init(this);
        classid = this.getIntent().getLongExtra("classid",0);
        context = this;

        progress = DialogUtils.createProgress(context);
        progress.show();

        final CourseListProtocol protocol = new CourseListProtocol();
        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(context, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, context)) {

                    if (null != protocol.courseList) {
                        curseList = protocol.courseList;
                        curseAdapter = new CourseAdapter(context, SelectCourseActivity.this);
                        curseAdapter.setData(protocol.courseList);
                        lvCurse.setAdapter(curseAdapter);
                    }
                }
            }
        });
    }

    private void  initview(){
        lvCurse = (ListView)findViewById(R.id.lv_select_course);
        btnOk = (Button)findViewById(R.id.btn_ok);
        lvCurse.setDivider(null);
    }

    private void setListener(){

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int header = 0;   //1 班主任  0 卸任班主任
                String courses = "";  //课程id ，逗号分隔
                listItemID.clear();
                for (int i = 0; i < curseAdapter.checkedList.size(); i++) {
                    if (curseAdapter.checkedList.get(i)) {
                        listItemID.add(i);
                        if (i == 0) {
                            header = 1;
                        } else {
                            courses += curseList.get(i).id + ",";
                        }
                    }
                }

                if (listItemID.size() == 0) {
                    Toast.makeText(context, R.string.teache_select_noselect, Toast.LENGTH_SHORT).show();
                } else {

                    courses = courses.substring(0, courses.lastIndexOf(","));

                    Log.e("tag", "courses=====" + courses + " classid:" + classid);

                    selectCourse(header, courses);
                }
            }
        });
    }

    @Override
    public void click(View v) {

    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        
    }

    /**
     * 加入班级网络请求
     */
    private void selectCourse(int header,String courses){
        progress = DialogUtils.createProgress(context);
        progress.show();

        final SelectCourseProtocol protocol = new SelectCourseProtocol(classid,header,courses);

        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(context, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, context)) {
                    Log.e("Tag", "返回加入班级信息.." + protocol.returnMap);
                    toClassDetail();
                }

            }
        });
    }

    private void  toClassDetail( ){
        Intent  intent =new Intent(context,ClassDetailActivity.class);
          intent.putExtra("classid",classid);
        this.startActivity(intent);
    }
}
